﻿
using DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace DAO
{
    public class LoaiPinDAO
    {
/*-------------------------LOAD----------------------------------------------------
LOADLOAIPIN
LOADLOAIPIN_THEOMA
*/

        public DataTable LoadLoaiPin()
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlDataAdapter _da = new SqlDataAdapter("LOADLOAIPIN", _cn);
            _da.SelectCommand.CommandType = CommandType.StoredProcedure;

            DataTable _dt = new DataTable();
            _da.Fill(_dt);
            _cn.Close();
            return _dt;
        }
        public DataTable LoadLoaiPin_TheoPin()
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlDataAdapter _da = new SqlDataAdapter("LOADLOAIPIN_THEOPIN", _cn);
            _da.SelectCommand.CommandType = CommandType.StoredProcedure;

            DataTable _dt = new DataTable();
            _da.Fill(_dt);
            _cn.Close();
            return _dt;
        }
        public DataTable LoadLoaiPin_TheoMa(int MaLPin)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlDataAdapter _da = new SqlDataAdapter("LOADLOAIPIN_THEOMA", _cn);
            _da.SelectCommand.CommandType = CommandType.StoredProcedure;
            _da.SelectCommand.Parameters.Add("@MaLPin", SqlDbType.Int).Value = MaLPin;
            DataTable _dt = new DataTable();
            _da.Fill(_dt);
            _cn.Close();
            return _dt;
        }

/*-------------------------INSERT----------------------------------------------------
THEMLOAIPIN
	@TenLoai nvarchar(10),
	@TienMoiWatt int
 */
        public int ThemLoaiPin(LoaiPinDTO lp)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlCommand cmd = new SqlCommand("THEMLOAIPIN", _cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@TenLoai", SqlDbType.NVarChar, 10));
            cmd.Parameters["@TenLoai"].Value = lp.TENLOAI;
            cmd.Parameters.Add(new SqlParameter("@TienMoiWatt", SqlDbType.Int));
            cmd.Parameters["@TienMoiWatt"].Value = lp.TIENMOIWATT;
            int _kq = cmd.ExecuteNonQuery();
            _cn.Close();
            return _kq;
        }
/*-------------------------UPDATE----------------------------------------------------
SUALOAIPIN @MaLPin int,
	@TenLoai nvarchar(10),
	@TienMoiWatt int
 */
        public int SuaLoaiPin(LoaiPinDTO lp)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlCommand cmd = new SqlCommand("SUALOAIPIN", _cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@MaLPin", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("@TenLoai", SqlDbType.NVarChar, 20));
            cmd.Parameters.Add(new SqlParameter("@TienMoiWatt", SqlDbType.Int));
            cmd.Parameters["@MaLPin"].Value = lp.MALPIN;
            cmd.Parameters["@TenLoai"].Value = lp.TENLOAI;
            cmd.Parameters["@TienMoiWatt"].Value = lp.TIENMOIWATT;
            int _kq = cmd.ExecuteNonQuery();
            _cn.Close();
            return _kq;
        }
/*-------------------------DELETE----------------------------------------------------
XOALOAIPIN @MaLPin int
*/
        public int XoaLoaiPin(int MaLoai)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlCommand cmd = new SqlCommand("XOALOAIPIN", _cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@MaLPin", SqlDbType.Int));
            cmd.Parameters["@MaLPin"].Value = MaLoai;
            int _kq = cmd.ExecuteNonQuery();
            _cn.Close();
            return _kq;
        }
    }
}
